<!--
PAGE DOCUMENTATION:
==================
This page contains a class of an object that mimics a FB multi-friends-selector.
Credit to Colin Kierans and dkogan (from FB developers forum). Great help!!!!
-->

<?php

class MultiFriendSelect
{
    public $num_columns = 5;
    public $num_rows = 5;
    public $selected_bg_image = ''; 
    public $hover_bg_image = ''; 
    
    //The number of links to show on either side of the current page
    //At two, when you are on page 6, you will see 4 5 6 7 8 as links
    public $page_bumper = 2;

    //Array of people to select right off the bat. If this is set
    //it will override the users_to_check
    public $selected_uids = array();
    
    //Array of uids not to include
    public $excluded_uids = array();

    //The maximum number of users a user can select. If false there is no max
    public $max_select = false;

    public $users_to_check = 0; //The initial number to select for the user. If you set selected_uids this is ignored.
    private $num_checked = 0; //The number of users that have been selected. Don't set this.

    //Displays a single page link
    public function displayPage($page, $text = NULL)
    {
        if($text == NULL)
        {
            $text = $page;
        }

        return '
            <a href="#" onclick="showPage(' . $page . ');">' . $text . '</a>';


    }

    //If the programmer passes in an array of arrays with the uid in the second tier array
    //then we need to reformat this array
    //This is primarily used for people who send in arrays that are in the FQL return format
    //example: array(45 => array('uid' => 45), 67 => array('uid' => 67'))
    public function convertUIDs($uids)
    {
        if(!is_array($uids))
        {
            return array();
        }

        //If this isn't an array then just return the uids as is cause it's the right format
        if(!is_array($uids[0]))
        {
            return $uids;
        }

        $new = array();

        foreach($uids as $array)
        {
            $new[] = $array['uid'];
        }

        return $new;
    }

    //Returns the javascript links for a certain page
    public function displayPageLinks($page, $div = true)
    {
        if($div == true)
        {
            $display .= '
            <div id="page_' . $page . '" style="display: ' . ($page == 1 ? "block" : "none") . '" class="page_container">';
        }

        $display .='
            <div class="selector_page_links clearfix">';

        $min_page = max(1, $page - $this->page_bumper);
        $max_page = min($this->num_pages, $page + $this->page_bumper);

        $start_user = ($page - 1) * $this->users_per_page + 1;
        $end_user = min($this->num_users, ($start_user + $this->users_per_page - 1));

        //Shows the current number of users you are seeing
        $display .=
        '
        <div class="user_counts">
        Showing user';

        if($start_user == $end_user)
        {
            $display .= ' ' . $start_user;
        }
        else
        {
            $display .= 's ' . $start_user . ' to ' . $end_user . ' of ' . $this->num_users;
        }

        $display .= "
        </div>
        <div class='the_links'>";

        if($page > 1)
        {
            $display .= $this->displayPage($page - 1, "Prev");
        }

        //Go
        for($i = $min_page; $i <= $max_page; $i++)
        {
            if($i == $page)
            {
                $display .= '
                <span class="current_page">' . $i . '</span>';
            }
            else
            {
                $display .= $this->displayPage($i);
            }
        }

        if($page < $this->num_pages)
        {
            $display .= $this->displayPage($page + 1, "Next");
        }

        $display .= '
        </div>';

        return $display . '
        </div>';
    }
    //Returns whether to check oof this user or not
    public function userChecked($uid)
    {
        if(in_array($uid, $this->selected_uids))
        {
            $this->num_checked++;
            return true;
        }

        if($this->num_checked < $this->users_to_check)
        {
            $this->num_checked++;
            return true;
        }

        return false;
    }

    //Displays the select for a user
    public function displayUser($uid)
    {
        $checked = $this->userChecked($uid);

        return '
            <a href="#" class="user_select ' . ($checked == true ? "selected" : "not") . '" onclick="toggleUserSelect(' . $uid . ');" id="selector_' . $uid . '">
                <fb:profile-pic size="square" linked="false" uid="' . $uid . '" />
                <br /><fb:name uid="' . $uid . '" linked="false" />
            <input type="checkbox" ' . ($checked == true ? "checked " : "") . 'style="display: none;" id="checkbox_' . $uid . '" name="selected_uids[' . $uid . ']" value="' . $uid . '" />
            </a>

        ';
    }

    public function displaySelector($users)
    {
        $this->selected_uids = $this->convertUIDs($this->selected_uids);
        $this->excluded_uids = $this->convertUIDs($this->excluded_uids);
        $users = $this->convertUIDs($users);

        //Remove users from the inputted ones that are in the excluded list
        if(count($this->excluded_uids) > 0)
        {
            foreach($users as $key => $uid)
            {
                //If this user is in the excluded list then remove it from the array of users to display
                if(in_array($uid, $this->excluded_uids))
                {
                    unset($users[$key]);
                }
            }
        }

        //Get some basic info that is mostly used in the paging function
        $this->num_users = count($users);
        $this->users_per_page = $this->num_columns * $this->num_rows;
        $this->num_pages = ceil($this->num_users / $this->users_per_page);

        //If they have supplied people to check then ignore the number to check
        if(count($this->selected_uids) > 0)
        {
            $this->users_to_check = 0;
        }

        $rows = 0;
        $columns = 0;
        $page = 1;

        $display .= '
        <div class="vin_selector clearfix">
            <div class="clearfix">
                <div class="vin_num_selected">
                    Friends Selected: <span id="vin_num_selected">' . $this->num_checked . '</span>
                </div>
                <div class="vin_all_button">
                    <a href="#" onclick="listCheckAll(true);">Select All</a>
                    <span class="pipe">|</span>
                    <a href="#" onclick="listCheckAll(false);">Clear All</a>
                </div>
            </div>

        ' . $this->displayPageLinks($page);

        //Go through each user that is entered and add them as a selectable
        foreach($users as $uid)
        {
            $display .= $this->displayUser($uid);

            $columns++;
            if($columns == $this->num_columns)
            {
                $columns = 0;
                $display .= '
                <div class="column_breaker"></div>';

                $rows++;

                if($rows == $this->num_rows)
                {
                    $rows = 0;

                    //Bottom page links
                    $display .= $this->displayPageLinks($page, false);

                    //Close the previous page thingy thingy thingy thingy thingy
                    $display .= '
                    </div>
                    <div class="row_breaker"></div>';

                    $page++;

                    $display .= $this->displayPageLinks($page);
                }
            }
        }

        return $display .= '
            </div>
        </div>' . $this->displayJS($users) . $this->displayCSS();
    }

    //Displays the javascript array that is used for select and clear all js functions
    public function displayUIDsArray($uids)
    {
        $display = '
            var all_uids = [';

        $first = true;
        foreach($uids as $uid)
        {
            if($first == false)
            {
                $display .= ',';
            }

            $display .= $uid;

            $first = false;
        }

        $display .= '];';

        $display .= '
        var selected_uids = [';

        $first = true;
        foreach($this->selected_uids as $uid)
        {
            if($first == false)
            {
                $display .= ',';
            }

            $display .= $uid;

            $first = false;
        }

        $display .= '];
        ';

        return $display;
    }

    //Returns the javascript for the selector
    public function displayJS($uids)
    {
        return "
<script>
var max_select = " . ($this->max_select == false ? $this->num_users : $this->max_select) . ";
var num_checked = " . ($this->num_checked ? $this->num_checked : 0) . ";
var last_page = 1;
function showPage(page)
{
    //Hide current page
    document.getElementById('page_' + last_page).setStyle('display', 'none');

    //Show selected page
    document.getElementById('page_' + page).setStyle('display', 'block');
    last_page = page;
}

function listCheckAll(value)
{
    for(i = 0; i < all_uids.length; i++)
    {
        var uid = all_uids[i];

        var ret = setUserSelect(uid, value);

        //If the setuserselect function returns false that means you have reached the maximum number
        //of users you are allowed to select
        if(ret == false)
        {
            return true;
        }
    }
}

function setUserSelect(uid, set)
{
    checkbox = document.getElementById('checkbox_' + uid);
    selector = document.getElementById('selector_' + uid);
    checked = checkbox.getChecked();
    //new Dialog().showMessage('Set', set);
    //new Dialog().showMessage('Checked?', checked);
    //new Dialog().showMessage('Num Checked', num_checked);

    if(set == true && checked == false)
    {
        if(num_checked >= max_select && num_checked > 0)
        {
            new Dialog().showMessage('Limit Reached', 'You can only select up to ' + max_select + ' people.');
            return false;

            //Add this uid to the array of selected uids
            selected_uids[uid] = uid;
        }

        num_checked++;
        checkbox.setChecked(true);
        selector.setClassName('user_select selected');
    }
    else
    {
        if(set == false && checked == true)
        {
            if(num_checked > 0)
            {
                num_checked--;
            }
            checkbox.setChecked(false);

            //Remove this uid to the array of selected uids
            //Dopesn't really remove but sets it to false
            selected_uids[uid] = 0;


            selector.setClassName('user_select not');
        }
    }

    document.getElementById('vin_num_selected').setTextValue(num_checked);

    return true;
}

function toggleUserSelect(uid)
{
    setUserSelect(uid, !document.getElementById('checkbox_' + uid).getChecked());
}

//This function loops through all users and if that user is selected it puts it into the array
//of selected users
function getSelectedUsers()
{
    var selected_uids = [];
    for(i = 0; i < all_uids.length; i++)
    {
        var uid = all_uids[i];

        if(document.getElementById('checkbox_' + uid).getChecked() == true)
        {
            selected_uids[selected_uids.length] = uid;
        }
    }
    
    return selected_uids;
}

" . $this->displayUIDsArray($uids) . "
document.getElementById('vin_num_selected').setTextValue(num_checked);
</script>";
    }

    public function displayCSS()
    {
        return '
<style>
.vin_selector
{
    margin: 0px auto 0px auto;
    width: 462px;
}

.vin_num_selected
{
    float: right;
    text-align: right;
}

.user_select img
{
    padding: 2px;
    border: 1px solid #CCCCCC;
    margin: 0px;
}

.user_select
{
    display: block;
    float: left;
    width: 80px;
    text-align: center;
    height: 90px;
    margin: 2px 4px;
    padding-top: 5px;
    overflow: hidden;
    background-position: 50% 50%;
}

.selected
{
    background-image: url(' . $this->selected_bg_image . ');
    color: #FFFFFF;
}

.selected:hover
{
    text-decoration: none;
}

.not:hover
{
    background-image: url(' . $this->hover_bg_image . ');
    text-decoration: none;
}

.not:hover img
{
    border-color: #FFFFFF;
}

.user_counts
{
    float: left;
}

.the_links
{
    text-align: right;
}

.the_links a
{
    margin: 0px 2px 0px 2px;
}

.column_breaker
{
    clear: left;
}

.row_breaker
{
    margin-bottom: 3px;
}

.current_page
{
    font-weight: bold;
}

.page_container
{
    padding: 5px 0px;
}

.selector_page_links
{
    padding: 5px 0px;
}
</style>';
    }
}
?>

